package com.example.mybatisplus.Interceptor.mybatis;

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 自定义插件
 * 执行顺序willDoXXX -> beforeXXX -> beforePrepare -> beforeGetBoundSql
 */
public class CustomInnerInterceptor implements InnerInterceptor {

    /**判断是否执行MappedStatement.query,如果返回true，代表执行查询操作，否则不执行该操作
     * @param executor 执行该sql语句的执行器
     * @param ms 该查询对应的MappedStatement对象
     * @param parameter 查询的方法参数值
     * @param rowBounds
     * @param resultHandler
     * @param boundSql 执行的sql语句
     * @return
     * @throws SQLException
     */
    //如果目标target是Executor，则会调用该方法
    @Override
    public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        System.out.println("willDoQuery");
        return InnerInterceptor.super.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }

    /**
     *在MappedStatement.query之前执行该方法。(还未进行query操作)
     */
    //如果目标target是Executor，则会调用该方法
    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        System.out.println("beforeQuery");
        InnerInterceptor.super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }

    @Override
    public boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        return InnerInterceptor.super.willDoUpdate(executor, ms, parameter);
    }

    @Override
    public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        InnerInterceptor.super.beforeUpdate(executor, ms, parameter);
    }
    //如果目标target不是执行器，则是StatementHandler，那么就会调用该方法。在执行prepare(Connection, Integer) 之前执行（sql语句还没有执行）
    @Override
    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
        System.out.println("beforePrepare");
        InnerInterceptor.super.beforePrepare(sh, connection, transactionTimeout);
    }
    //如果目标target不是执行器，则是StatementHandler，那么就会调用该方法。在执行的getBoundSql()之前执行（sql语句还没有执行）
    @Override
    public void beforeGetBoundSql(StatementHandler sh) {
        System.out.println("beforeGetBoundSql");
        InnerInterceptor.super.beforeGetBoundSql(sh);
    }

    @Override
    public void setProperties(Properties properties) {
        System.out.println("setProperties");
        System.out.println(properties);
        InnerInterceptor.super.setProperties(properties);
    }
}
